﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using Models;
using BLL;
using System.Text;
using System.Net;

namespace CSMF
{
    public partial class j01 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLType();
            }
        }

        private void BindDDLType()
        {
            IList<VideoType> types = VideoTypeManager.GetAllVideoTypes();
            cbVideoType.Items.Clear();
            foreach (VideoType type in types)
            {
                ListItem li = new ListItem();
                li.Value = type.Title;
                li.Text = "&nbsp;" + type.Title + "&nbsp;";
                cbVideoType.Items.Add(li);
            }
        }


        ///<summary>
        ///发送邮件
        ///</summary>
        public void SendMail(string title, StringBuilder sb)
        {
            string mailfrom = "join@csmff.com";
            SmtpClient smtp = new SmtpClient();
            //smtp.UseDefaultCredentials = true;
            //smtp.Credentials = new NetworkCredential("join@csmff.com", "j2o0i1n2");
            MailMessage message = new MailMessage(mailfrom, mailfrom);
            message.IsBodyHtml = true;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = title;
            message.Body = sb.ToString();
            smtp.Send(message);
            message.Dispose();
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                if (txtTitle.Value != null)
                {
                    if (txtJianjie.Text.Trim() != "")
                    {
                        if (txtLianxiren.Value != null)
                        {
                            if (txtLianxifangshi.Value != null)
                            {
                                string videoType = "";
                                for (int i = 0; i < cbVideoType.Items.Count; i++)
                                {
                                    if (cbVideoType.Items[i].Selected == true)
                                    {
                                        if (videoType == "")
                                        {
                                            videoType = cbVideoType.Items[i].Value;
                                        }
                                        else
                                        {
                                            videoType += "," + cbVideoType.Items[i].Value;
                                        }
                                    }
                                }
                                if (videoType != "")
                                {
                                    string title = "微力十足参赛报名";
                                    StringBuilder sb = new StringBuilder();
                                    sb.Append("详细内容如下：<br><br>");
                                    sb.Append("参赛作品名：" + Convert.ToString(txtTitle.Value) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;片长:" + Convert.ToString(txtPianchang.Value) + "<br><br>");
                                    sb.Append("视频参数：" + Convert.ToString(txtCanshu.Value) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格式:" + Convert.ToString(txtGeshi.Value) + "<br><br>");
                                    sb.Append("作品类型：" + videoType + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;完成时间:" + Convert.ToString(txtTime.Value) + "<br><br><br><br>");
                                    sb.Append("团队名称：" + Convert.ToString(txtTeamtitle.Value) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;导演:" + Convert.ToString(txtDaoyan.Value) + "<br><br>");
                                    sb.Append("编辑：" + Convert.ToString(txtBianji.Value) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;摄影:" + Convert.ToString(txtSheying.Value) + "<br><br>");
                                    sb.Append("后期：" + Convert.ToString(txtHouqi.Value) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;其它:" + Convert.ToString(txtQita.Value) + "<br><br>");
                                    sb.Append("作品简介：" + txtJianjie.Text + "<br><br>");
                                    sb.Append("---------------------------------------------------------------------- <br><br>");
                                    sb.Append("网盘下载链接：" + Convert.ToString(txtLink.Value) + "<br><br>");
                                    sb.Append("联络人：" + Convert.ToString(txtLianxiren.Value) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;联络人联系方式:" + Convert.ToString(txtLianxifangshi.Value) + "<br><br>");
                                    sb.Append("联络人联系地址：" + Convert.ToString(txtLianxidizhi.Value) + "<br><br><br><br>");
                                    sb.Append("---------------------------------------------------------------------- <br><br>");
                                    sb.Append("(这是一封自动产生的email，请勿回复。) <br><br>");
                                    sb.Append("---------------------------------------------------------------------- <br><br>");
                                    SendMail(title, sb);

                                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('报名参赛成功。');", true);
                                }
                                else
                                {
                                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请选择作品类型。');", true);
                                }
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请填写联络人联系方式。');", true);
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请填写联络人。');", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请填写作品简介。');", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请填写参赛作品名。');", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请先阅读“海峡两岸微电影大赛-微力十足-参赛细则”。');", true);
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtTitle.Value = null;
            txtPianchang.Value = null;
            txtCanshu.Value = null;
            txtGeshi.Value = null;
            for (int i = 0; i < cbVideoType.Items.Count; i++)
            {
                cbVideoType.Items[i].Selected = false;
            }
            txtTime.Value = null;
            txtTeamtitle.Value = null;
            txtDaoyan.Value = null;
            txtBianji.Value = null;
            txtSheying.Value = null;
            txtHouqi.Value = null;
            txtQita.Value = null;
            txtJianjie.Text = "";
            txtLink.Value = null;
            txtLianxiren.Value = null;
            txtLianxifangshi.Value = null;
            txtLianxidizhi.Value = null;
        }

    }
}
